package com.spring.demo.springdemo.entity;

import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @Author RenPu
 * @Date 2023/3/20 15:44
 * @Version 1.0
 * @Description:
 **/

@Data
@Component
public class ResponseResult {

    private Integer code;

    private String msg;

    public ResponseResult() {
        System.out.println("构造方法被执行。。。。。。。");
    }

    @PostConstruct
    public void init(){
        System.out.println("注解PostConstruct标注的方法，被执行。。。。。。。");
    }

    static {
        System.out.println("静态代码块的方法被执行。。。。。。。。");
    }



    public ResponseResult(Integer code, String returnMsg) {
        this.code=code;
        this.msg=returnMsg;
    }




    // define a helper method to filter by a specific property
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    public static void main(String[] args) {

       List<ResponseResult> list = new ArrayList<>();
        list.addAll((Arrays.asList(new ResponseResult(500,"你好"),
                new ResponseResult(200,"他好"),
                new ResponseResult(404,"你好"),
                new ResponseResult(500,"你好"),
        new ResponseResult(404,"everyone好"))));



        List<ResponseResult> uniqueObjects = list.stream()
                .filter(distinctByKey(obj -> obj.getCode()))
                .filter(distinctByKey(obj -> obj.getMsg()))
                .collect(Collectors.toList());

        System.out.println(uniqueObjects);
    }



}
